using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Lexalytics;

namespace SalienceEngineDemo
{
    public partial class ctrlGetEntities : UserControl, DemoControl
    {
        public RichTextBox oTextBox;
        List<SalienceEntity> oResults = new List<SalienceEntity>();
        List<SalienceEntity> oGridResults = new List<SalienceEntity>();
        private bool bQuoteMode = false;
        private bool bUserEntityMode = false;

        public ctrlGetEntities(RichTextBox oTextBox)
        {
            InitializeComponent();
            this.oTextBox = oTextBox;
            EnableProcessButton();
        }

        public void setQuoteMode(bool bMode)
        {
            bQuoteMode = bMode;
        }

        public void setUserEntityMode(bool bMode)
        {
            bUserEntityMode = bMode;
            if (bMode)
            {
                ctrlLabel.Text = "Get User-Defined Entities";
                EnableProcessButton();
            }
            else
            {
                ctrlLabel.Text = "Get Named Entities";
                EnableProcessButton();
            }
        }

        public void processText()
        {
            gridResults.Rows.Clear();
            try
            {
                string[] aText = {"</body>"};
                string[] parts = this.oTextBox.Text.Split(aText,StringSplitOptions.None);
                foreach (string s in parts)
                {
                    SalienceDemo.oSalienceSession.PrepareText(s);

                    oResults = new List<SalienceEntity>();
                    oGridResults = new List<SalienceEntity>();

                    if (!bUserEntityMode)
                    {
                        oResults = SalienceDemo.oSalienceSession.GetNamedEntities();
                    }
                    else
                    {
                        oResults = SalienceDemo.oSalienceSession.GetUserDefinedEntities();
                    }
                }

                if (oResults.Count > 0)
                {
                    //Put the results in the grid
                    int nCount = 0;
                    foreach (SalienceEntity oEntity in oResults)
                    {
                        gridResults.Rows.Add();
                        gridResults.Rows[nCount].Cells[0].Value = oEntity.sNormalizedForm;
                        if (oEntity.sType != "Pattern")
                        {
                            gridResults.Rows[nCount].Cells[1].Value = oEntity.sType;
                        }
                        else
                        {
                            gridResults.Rows[nCount].Cells[1].Value = oEntity.sLabel;
                        }
                        gridResults.Rows[nCount].Cells[2].Value = oEntity.fSentimentScore;
                        gridResults.Rows[nCount].Cells[3].Value = oEntity.nEvidence;
                        gridResults.Rows[nCount].Cells[4].Value = oEntity.nCount;
                        gridResults.Rows[nCount].Tag = nCount;
                        nCount++;

                        // Add to grid results for details dialog
                        oGridResults.Add(oEntity);
                    }
                    gridResults.AutoResizeRows();
                    gridResults.Sort(gridResults.Columns[4], ListSortDirection.Descending);
                }
                else
                {
                    if (bUserEntityMode)
                        MessageBox.Show("No User-defined Entities Available", "GetUserDefinedEntities()");
                    else
                        MessageBox.Show("No Entities Available", "GetNamedEntities()");
                }
            }
            catch (SalienceException e)
            {
                MessageBox.Show(e.Message, "Salience Demo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnProcess_Click(object sender, EventArgs e)
        {
            oResults.Clear();
            oGridResults.Clear();
            processText();
        }

        //Clean up
        private void ctrlGetEntities_VisibleChanged(object sender, EventArgs e)
        {
            gridResults.Rows.Clear();
        }

        private void gridResults_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (!bQuoteMode)
            {
                string myContent = this.oTextBox.Text;
                Dialogs.EntityDetails dlgEntityDetails = new SalienceEngineDemo.Dialogs.EntityDetails(gridResults, oGridResults, myContent);
                dlgEntityDetails.ShowDialog();
            }
        }

        #region DemoControl interface methods
        public void btnOptions_Click(object sender, EventArgs e)
        {
            ((SalienceDemo)this.Parent).StartOptionsDialog((int)Dialogs.Options.OptionSections.ENTITIES);
            EnableProcessButton();
        }

        public void SetFontSize()
        {
            this.gridResults.DefaultCellStyle.Font = new Font(this.oTextBox.Font.FontFamily, Properties.Settings.Default.fFontSize);
            this.gridResults.AutoResizeRows();
        }

        public void EnableProcessButton()
        {
            // If we are in user entity mode, Process button enabled by existence of user entity list and content to process, otherwise just based on content
            if (bUserEntityMode)
            {
                btnProcess.Enabled = (System.IO.File.Exists(SalienceDemo.oConfig.activeSettings().settings["UserEntityList"]) &&
                                        (oTextBox.Text.Trim().Length > 0));
            }
            else
            {
                btnProcess.Enabled = (oTextBox.Text.Trim().Length > 0);
            }
        }
        #endregion
    }
}
